<%@ page contentType="text/xml;charset=utf-8"
	import="java.sql.Connection, java.sql.PreparedStatement, java.sql.ResultSet
	, git.bankview.sl.db.DBConnectionFactory, java.util.ArrayList
	, git.bankview.wl.auth.userGroups.TransCheck
	, git.bankview.wl.bean.TaskSchedulerBean
	, git.bankview.sl.schedule.TaskScheduler"
%><%@ include file="/jsp/inc/pageUtils.jsp"
%><% request.setCharacterEncoding("utf-8");
	response.addHeader("Cache-Control", "no-cache"); %>
<jsp:useBean id="oLogon" class="git.bankview.wl.bean.LogonBean" scope="session" />
<handleTaskDelete>
<%
	String msg = "检查用户权限";
	try
	{
		//TransCheck.check(oLogon.getUsername(), "90060", 255);
		
		msg = "获得任务ID参数";
		int taskid = Integer.parseInt(request.getParameter("taskid"));
		msg = "获得任务日期";
		java.sql.Date datekey = sqlDate(request.getParameter("datekey"));
		
		msg = "获取计划任务调度实例";
		TaskScheduler ts = TaskScheduler.getInstance();
		msg = "检查计划任务调度实例状态";
		int tsStatus = ts.getStatus();

		if (null != ts.getSchdDate() && datekey.compareTo(ts.getSchdDate()) <= 0 && TaskScheduler.NEXTSCHD != tsStatus)
			throw new java.lang.IllegalStateException("当前任务日期的计划任务实例正在运行(" + tsStatus + ":" + ts.getStatusName(tsStatus) + ")");
			
		msg = "获得数据库连接";
		Connection conn = DBConnectionFactory.getCoreConnection();
		try
		{
			ArrayList depTaskList = new ArrayList();
			ArrayList subTaskList = new ArrayList();
			PreparedStatement ps = conn.prepareStatement("select deptaskid from taskdependence where datekey=? and taskid=?");
			try	{ ps.setDate(1, datekey);
			ps.setInt(2, taskid);
			ResultSet rs = ps.executeQuery();
			try {
			while(rs.next())
			{
				depTaskList.add(rs.getString(1));
			}
			} finally { rs.close(); }
			} finally { ps.close();	}
			
			ps = conn.prepareStatement("select taskid from taskdependence where datekey=? and deptaskid=?");
			try	{ ps.setDate(1, datekey);
			ps.setInt(2, taskid);
			ResultSet rs = ps.executeQuery();
			try {
			while(rs.next())
			{
				subTaskList.add(rs.getString(1));
			}
			} finally { rs.close(); }
			} finally { ps.close();	}
			
			// clean old rec
			msg = "清理任务依赖关系";
			ps = conn.prepareStatement("delete from taskdependence where datekey=? and (taskid=? or deptaskid = ?)");
			try	{ ps.setDate(1, datekey);
			ps.setInt(2, taskid);
			ps.setInt(3, taskid);
			ps.executeUpdate();
			} finally { ps.close();	}
	
			msg = "清理任务计划";
			ps = conn.prepareStatement("delete from taskschedule where datekey=? and taskid=?");
			try	{ ps.setDate(1, datekey);
			ps.setInt(2, taskid);
			ps.executeUpdate();
			} finally { ps.close();	}
			
			msg = "更新任务依赖关系";
			ps = conn.prepareStatement("insert into taskdependence (datekey, taskid, deptaskid) values (?,?,?)");
			try {
			for (int i = 0; i < depTaskList.size(); ++i)
			{
				for (int j = 0; j < subTaskList.size(); ++j)
				{
					ps.setDate(1, datekey);
					ps.setString(2, (String)subTaskList.get(j));
					ps.setString(3, (String)depTaskList.get(i));
				}
			}
			ps.executeBatch();
			} finally { ps.close(); }
%>
	<result>0</result>
	<message>计划任务信息删除成功</message>
	<remark></remark>
<%
		}
		finally
		{
			conn.close();
		}
	}
	catch(Exception e)
	{
		e.printStackTrace();
%>
	<result>-1</result>
	<message><%=msg %>失败</message>
	<reason><![CDATA[<%= e.getMessage()%>]]></reason>
	<remark><![CDATA[<% e.printStackTrace(new java.io.PrintWriter(out)); %>]]></remark>
<%
	}
%>
</handleTaskDelete>
